<?php session_start(); ?>
<html>
<head>

<style>
body
{

font-family: "Lucida Scourseadd Unicode", "Lucida Grande", Scourseadd-Serif;
color: #036;

}
</style>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 


<script type="text/javascript" src="jquery-1.4.1.js"></script>
<style>
body
{

font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
color: #036;

}
</style>

<script>
function fillDropDownLists(htmlobj) {
   var xmlhttp;

   if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp = new XMLHttpRequest();
   }
   else
   {// code for IE6, IE5
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function()
   {
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
       {
           //TODO: validate 5000 and 6000 tables
           var ddl_options = "<option value=''></option>";
           var courses_string = xmlhttp.responseText;
           
           courses_string = courses_string.substring(0, courses_string.length - 1);
           var course_codes = courses_string.split(",");

           for (var i = 0; i < course_codes.length; i++) {
               ddl_options += "<option value='" + course_codes[i] + "'>" + course_codes[i] + "</option>";
           }

           htmlobj.innerHTML = ddl_options;
           //document.getElementsByName("classNumber5_ti")[0].innerhtml = ddl_options;

       }
   }
   xmlhttp.open("GET", "getuser1.php", true);
   xmlhttp.send();
}


function fillRow(htmlobj)
{
//alert(htmlobj)
var e = htmlobj;


var objId = htmlobj.getAttribute('id');
var stringLength = objId.length; // this will be 16
var lastChar = objId.charAt(stringLength - 1);
//alert(lastChar);
var strUser = e.options[e.selectedIndex].value;

   if (strUser=="") {
   return;} 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    var responseArray = xmlhttp.responseText.split(" $$ ");
    
    
    //document.getElementById("courseName6k3").innerHTML=responseArray[0];
    document.getElementById("courseName"+lastChar).value =responseArray[0];
     document.getElementById("courseSemester"+lastChar).value =responseArray[1];
     document.getElementById("courseYear"+lastChar).value =responseArray[2];
    document.getElementById("courseGrade"+lastChar).value =responseArray[3];
     document.getElementById("courseArea"+lastChar).value =responseArray[4];


	
//     document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getCourseContent.php?q="+strUser,true);
  xmlhttp.send();
}



</script>

</head>

<body>


<!-- header.css  -->
<div id = "headerwrapper">

	<table id = "headerTable">
		<tr>
			<td>
				<img id = "unoLogo" src="Logo1.jpg" /> 
			</td>
			<td>
				<font size ="16"> Department of Computer Science </font>
			</td>
			<td>
			</td>

			
		</tr>
		
	</table>


<div id = "bodywrapper">
	<center><b><u>Graduate Checklist - Thesis option</u></b></center>
	<h4 align ='right' style='margin-right:5px margin-top:10px;'> 
	<a href='logout.php'>Logout Here </a>
	</div>
	
<!--courseSelectionForm.css -->

<div>
<form action = "thesis.php" method ="post" id = "courseSelectionThesForm">
<?php
$con = mysqli_connect('localhost','root','','db4208');
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
}

$useremail=$_SESSION['email'];
// $i = 1;
// $alpha = array(
// 1=>"A",
// 2=>"B",
// 3=>"C",
// 4=>"D",
// );
//$query="SELECT d.Firstname,d.Lastname,u.user_id FROM `users` u join data d on u.user_id = d.Studentid WHERE user_email='$useremail'";
$flag=0;
$query="SELECT DISTINCT(d.Firstname) FROM `users` u join data d on u.user_id = d.Studentid WHERE user_email='$useremail'";
$result = mysqli_query($con,$query);
$query1="SELECT DISTINCT(d.Lastname) FROM `users` u join data d on u.user_id = d.Studentid WHERE user_email='$useremail'";
$result1 = mysqli_query($con,$query1);
$query2="SELECT DISTINCT(d.Studentid) FROM `users` u join data d on u.user_id = d.Studentid WHERE user_email='$useremail'";
$result2 = mysqli_query($con,$query2);
 echo "<table>
<tr>
 <td>First Name</th>
 <td>Last Name</th>
 <td>Student ID</th>
 </tr>";
 echo "<tr>";
// // if($flag==0){
// echo"<td><input type='text'size = '15' name='firstname'></td>";
// echo"<td><input type='text'size = '15' name='lastname'></td>";
// echo"<td><input type='text'size = '15' name='studentid'></td>";
// 
// echo "</tr>";
// }
while ($row = mysqli_fetch_array($result)) {
 $flag++;
//echo $flag;

while ($row1 = mysqli_fetch_array($result1)){
while ($row2 = mysqli_fetch_array($result2))
{

 $values1 = array_values($row);
  $values2 = array_values($row1);
  $values3 = array_values($row2);
if($flag==1){
// echo "<table>
// <tr>
// <td>First Name</th>
// <td>Last Name</th>
// <td>Student ID</th>
// </tr>";
//echo "<td>" "First Name" "</td>";

echo "<tr>";
echo"<td><input type='text'size = '15' name='firstname' value='".$values1[0]."'></td>";
echo"<td><input type='text'size = '15' name='lastname' value='".$values2[0]."'></td>";
echo"<td><input type='text'size = '15' name='studentid' value='".$values3[0]."'></td>";

echo "</tr>";
 }
}
}
}

echo "<tr>";
 if($flag==0){
echo"<td><input type='text'size = '15' name='firstname'></td>";
echo"<td><input type='text'size = '15' name='lastname'></td>";
echo"<td><input type='text'size = '15' name='studentid'></td>";

echo "</tr>";
}
?>
</div>
<!-- 4000 level course table -->
<div class = "courseTableWrapper">

<div class ="courseTableCaption"> 5000 level </div>
<div class ="courseTableCaptionNote"> Note: At most 3 5000 level courses count towards the degree requirements.</div>

	<table id = "courseSelectionTable5000">

		<th>Number</th>
		<th>Title</th>
		<th>Semester</th>
		<th>Year</th>
		<th>Grade</th>
		<th>Area</th>
<?php
$con = mysqli_connect('localhost','root','','db4208');
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
}
//session_start();
$useremail=$_SESSION['email'];
$i = 1;
$alpha = array(
1=>"A",
2=>"B",
3=>"C",
4=>"D",
5=>"E",
6=>"F",
7=>"G",
8=>"H",
9=>"I",
10=>"J",
);
$sql="SELECT u.*, d.* FROM `users` u join data d on u.user_id = d.Studentid WHERE user_email='$useremail'";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
?>

<tr id="<?=$alpha[$i];?>">
<td>
<select id="classNumber<?=$alpha[$i];?>" name="aform<?=$i;?>" onclick="fillDropDownLists(this)" onchange="fillRow(this)">
 <option selected="selected"><?= $row['c_number']?></option>
 
</select> 
</td>

<td class = "courseTitle"><input type ="text" name ="courseName<?=$i;?>" size = "40" id ="courseName<?=$alpha[$i];?>" value="<?= $row['c_title']?>"/></td>
<td class = "courseSemester"><input type="text" size="8" name="courseSemester<?=$i;?>" id= "courseSemester<?=$alpha[$i];?>" value="<?= $row['c_sem']?>"></td>
<td class = "courseYear"><input type="text" size="8" name="couseYear<?=$i;?>" id= "courseYear<?=$alpha[$i];?>" value="<?= $row['c_year']?>"></td>
<td class = "courseGrade"><input type="text" size="5" name="courseGrade<?=$i;?>" id= "courseGrade<?=$alpha[$i];?>" value="<?= $row['c_grade']?>"></td>
<td class = "courseArea"><input type="text" size="5" name="courseArea<?=$i;?>" id= "courseArea<?=$alpha[$i];?>" value="<?= $row['c_area']?>"></td>
<td class = " Add"> <input type="button" value="Add field" name="Add" class="add" id="addButton" />
</tr>
 
<!--  <tr id="temp"  style="{display : none;}"> -->
  <tr id="temp" style="display:none">
  		<!-- 
<td><select id="classNumber" name="aform" onclick="fillDropDownLists(this)" onchange="fillRow(this)">
 <option selected="selected"></option>
 
</select> </td>
 -->

 		<td ><input type="text" name="coursenum" value="" size="15"></td>
		<td class="courseTitle"> <input type="text" name="coursename" value="" size="40"></td>
		<td class="courseSemester"><input type="text" name="coursesem" value="" size="8"></td>
		<td class="courseYear"> <input type="text" name="courseyear" value="" size="8"></td>
		<td class="courseGrade"><input type="text" name="coursegrade" value="" size="5"></td>
		<td class="courseArea"> <input type="text" name="coursearea" value="" size="5"></td>
		</tr>
		

		
<?  
	$i++;
	$values = array_values($row);
}
 //echo $values[0];
	for(;$i<14;$i++){


if( $i==4)
{
?>
<div class = "courseTableWrapper">

<div class = "courseTableCaption">6000 level courses </div>
<div class = "courseTableCaptionNote"> Note: The following 6000-level courses DO NOT count towards the degree requirements: CSCI 6001 &#44; CSCI 6090</div>

<table id = "courseSelectionTable6000">

		<th>Number</th>
		<th>Title</th>
		<th>Semester</th>
		<th>Year</th>
		<th>Grade</th>
		<th>Area</th>
</div>

 <?}?>

<tr id="<?=$alpha[$i];?>">
		<td>
<select id="classNumber<?=$alpha[$i];?>" name="aform<?=$i;?>" onclick="fillDropDownLists(this)" onchange="fillRow(this)">
 <option selected="selected"><?= $row['c_number']?></option>
 
</select> 
</td>

<td class = "courseTitle"><input type ="text" name ="courseName<?=$i;?>" size = "40" id ="courseName<?=$alpha[$i];?>" /></td>
<td class = "courseSemester"><input type="text" size="8" name="courseSemester<?=$i;?>" id= "courseSemester<?=$alpha[$i];?>" ></td>
<td class = "courseYear"><input type="text" size="8" name="couseYear<?=$i;?>" id= "courseYear<?=$alpha[$i];?>"></td>
<td class = "courseGrade"><input type="text" size="5" name="courseGrade<?=$i;?>" id= "courseGrade<?=$alpha[$i];?>" ></td>
<td class = "courseArea"><input type="text" size="5" name="courseArea<?=$i;?>" id= "courseArea<?=$alpha[$i];?>"></td>
<!-- <td class = " Add"> <input type="button" value="Add field" class="add" id="addButton" /> -->
</tr>
<?}?>
				
</table>

</div><!-- end courseTableWrapper div -->
</table>

</div><!-- end courseTableWrapper div -->


<div class = "courseTableWrapper">

<div class = "courseTableCaption">7000 level thesis hours </div>
<div class = "courseTableCaptionNote">Note: At most 6 hours of Thesis Research count towards the degree requirements</div>

<table id = "courseSelectionTable7000">

	
		
		<th>Semester</th>
		<th>Year</th>
	
		
		<tr>
		 	
			<td class = "courseSemester">  
				<select name = "courseSemester7k1">
					  <option> </option>
					  <option>Fall</option>
					  <option>Spring</option>
					  <option>Summer</option>
					  
				</select>
			</td>
			<td class = "courseYear">
				<select name = "courseYear7k1">
					<option> </option>
					<option>2014</option>
					<option>2013</option>
					<option>2012</option>
					<option>2011</option>
					<option>2010</option>
					<option>2009</option>
					<option>2008</option>
					<option>2007</option>
					<option>2006</option>
					<option>2005</option>
				</select>
			</td>
		
			
		</tr>
		<tr>
		 
			<td class = "courseSemester">  
				<select name = "courseSemester7k2">
					  <option> </option>
					  <option>Fall</option>
					  <option>Spring</option>
					  <option>Summer</option>
					  
				</select>
			</td>
			<td class = "courseYear">
				<select name = "courseYear7k2">
					<option> </option>
					<option>2014</option>
					<option>2013</option>
					<option>2012</option>
					<option>2011</option>
					<option>2010</option>
					<option>2009</option>
					<option>2008</option>
					<option>2007</option>
					<option>2006</option>
					<option>2005</option>
				</select>
			</td>
		
			
		</tr>

</table>


<div id = "thesisInformationDiv">
		<label>Thesis Title:</label>
		<input type ="text" size = "40" name ="thesisTitle" />
		<label>Thesis Advisor:</label>
		<input type ="text" size = "20" name ="thesisAdvisor" />	
</div>

</div> <!-- end courseTableWrapperDiv -->
</table>

</br>


<input type = "submit" id = "formSubmitButton" name ="formSubmitButton" value ="Save"/>


</form>

</body>


</html>
<?php

mysql_connect("localhost","root","");
mysql_select_db("db4208");

if(isset($_POST['formSubmitButton'])){

foreach (range(1, 14) as $letter) {
 if(!isset($_POST['courseSemester'.$letter]) || $_POST['courseSemester'.$letter] == ""){
 	continue;
 } 
 
 $user_fname=$_POST['firstname'];
 $user_lname=$_POST['lastname'];
 $user_sid=$_POST['studentid'];
 $user_cnumber1=$_POST['aform'.$letter];
 $user_ctitle1 = $_POST['courseName'.$letter];
 $user_csem1 = $_POST['courseSemester'.$letter];
 $user_cyear1 = $_POST['couseYear'.$letter];
 $user_cgrade1 = $_POST['courseGrade'.$letter];
 $user_carea1 = $_POST['courseArea'.$letter];
 
 $DeleteQuery="DELETE FROM `data` WHERE Studentid='$user_sid'";
  mysql_query($DeleteQuery);

}


foreach (range(1, 14) as $letter) {
 if(!isset($_POST['courseSemester'.$letter]) || $_POST['courseSemester'.$letter] == ""){
 	continue;
 } 
 
 
 $user_fname=$_POST['firstname'];
 $user_lname=$_POST['lastname'];
 $user_sid=$_POST['studentid'];
 $user_cnumber1=$_POST['aform'.$letter];
 $user_ctitle1 = $_POST['courseName'.$letter];
 $user_csem1 = $_POST['courseSemester'.$letter];
 $user_cyear1 = $_POST['couseYear'.$letter];
 $user_cgrade1 = $_POST['courseGrade'.$letter];
 $user_carea1 = $_POST['courseArea'.$letter];
 
// $DeleteQuery="DELETE FROM `data` WHERE Studentid='$user_sid'";
//  mysql_query($DeleteQuery);

echo $query="insert into data(Firstname,Lastname,Studentid,c_number,c_title,c_sem,c_year,c_grade,c_area) values ('$user_fname','$user_lname','$user_sid','$user_cnumber1','$user_ctitle1','$user_csem1','$user_cyear1','$user_cgrade1','$user_carea1')";
if(mysql_query($query)){
 echo " <script> alert('Data saved in database')</script>";
 
 }
}
}

?>

<script>

$(".add").click(function (event) {
event.stopPropagation();
$("#courseSelectionTable5000").append($("#temp").clone().removeAttr("id").attr("id",'F').show());

});</script>



<!-- 
<?php
$con=mysql_connect("localhost","root","","db4208")
or die ("coultnot connect");
mysql_select_db("test",$con)
or die("database not found");
$i=0;
 
foreach($_POST['courseadd'] as $value )
{echo "<table border='1'><tr><td>";
echo $i+1;
echo "</td><td>".$value."</td><td>".$_POST['courseadd'][$i]."</td></tr></table><br/>";
$i++;
//mysql_query("INSERT INTO record values('".$value."','".$_POST['courseadd'][$i]."'");
}
?>
 -->
 
 <?php
 
 mysql_connect("localhost","root","");
mysql_select_db("db4208");

if(isset($_POST['formSubmitButton'])){
 $user_fname1=$_POST['firstname'];
 $user_lname1=$_POST['lastname'];
 $user_sid1=$_POST['studentid'];
 $user_cnumber=$_POST['coursenum'];
 $user_ctitle = $_POST['coursename'];
 $user_csem = $_POST['coursesem'];
 $user_cyear = $_POST['courseyear'];
 $user_cgrade = $_POST['coursegrade'];
 $user_carea = $_POST['coursearea'];
 
 echo $query="insert into data(Firstname,Lastname,Studentid,c_number,c_title,c_sem,c_year,c_grade,c_area) values ('$user_fname1','$user_lname1','$user_sid1','$user_cnumber','$user_ctitle','$user_csem','$user_cyear','$user_cgrade','$user_carea')";
if(mysql_query($query)){
 echo " <script> alert('Data saved in database')</script>";
 
 }
 
 }
 
 ?>
 
